﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    public class RecipeManagementPageViewModel : ViewModelBase
    {
        public string DeleteSource;

        #region 分页使用
        /// <summary>
        /// 每页数量
        /// </summary>
        private int itemCount = 15;

        /// <summary>
        /// 总数量
        /// </summary>
        private int totalDataCount = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageNumber
        {
            get
            {
                int totalPageNumber = totalDataCount / itemCount;
                if (totalDataCount % itemCount > 0)
                {
                    totalPageNumber++;
                }
                return totalPageNumber;

            }
        }
        /// <summary>
        /// 总数据量
        /// </summary>
        public int TotalDataCount
        {
            get { return totalDataCount; }
            set
            {
                totalDataCount = value;
                RaisePropertyChanged("TotalDataCount");
                RaisePropertyChanged("TotalPageNumber");
            }
        }

        /// <summary>
        /// 当前所在页数
        /// </summary>
        private int _CurrentPageNumber = 1;
        /// <summary>
        /// 当前所在页数
        /// </summary>
        public int CurrentPageNumber
        {
            get { return _CurrentPageNumber; }
            set
            {
                _CurrentPageNumber = value;
                RaisePropertyChanged("CurrentPageNumber");
            }
        }
        /// <summary>
        /// 上一页页数
        /// </summary>
        public int PreviousPageNumber
        {
            get
            {
                if (_CurrentPageNumber - 1 > 0)
                {
                    return _CurrentPageNumber - 1;
                }
                return 1;
            }

        }
        /// <summary>
        /// 下一页页数
        /// </summary>
        public int NextPageNumber
        {
            get
            {
                if (_CurrentPageNumber + 1 <= TotalPageNumber)
                {
                    return _CurrentPageNumber + 1;
                }
                return _CurrentPageNumber;
            }

        }

        /// <summary>
        /// 选中的项目
        /// </summary>
        private RecipeTable _SelectedItemModel;
        /// <summary>
        /// 选中的项目
        /// </summary>
        public RecipeTable SelectedItemModel
        {
            get { return _SelectedItemModel; }
            set
            {
                _SelectedItemModel = value;
                RaisePropertyChanged("SelectedItemModel");
            }
        }

        /// <summary>
        /// 分页页码集合
        /// </summary>
        private ObservableCollection<PageNumerModel> _PageNumers = new ObservableCollection<PageNumerModel>();
        /// <summary>
        /// 分页页码集合
        /// </summary>
        public ObservableCollection<PageNumerModel> PageNumers
        {
            get => _PageNumers;
            set
            {
                _PageNumers = value;
                RaisePropertyChanged("PageNumers");
            }
        }
        #endregion 分页使用
        /// <summary>
        /// 跳转到指定页面命令
        /// </summary>
        public DelegateCommand GoToPageByNumberCommand { get; set; }
        /// <summary>
        /// 跳转到指定页面并关闭AllPageNumberPanel窗体命令
        /// </summary>
        public DelegateCommand GoToPageByNumberAndCloseAllPageNumberPanelCommand { get; set; }

        /// <summary>
        /// 显示更改到指定页面命令
        /// </summary>
        public DelegateCommand ChangeCurrentPageNumberCommand { get; set; }
        /// <summary>
        /// 取消命令
        /// </summary>
        public DelegateCommand CancelCommand { get; set; }

        /// <summary>
        /// 取消删除命令
        /// </summary>
        public DelegateCommand CancelDeleteCommand { get; set; }

        /// <summary>
        /// 确认删除命令
        /// </summary>
        public DelegateCommand ConfirmDeleteCommand { get; set; }

        /// <summary>
        /// 主列表数据
        /// </summary>
        private ObservableCollection<RecipeTable> _DataCollection;
        /// <summary>
        /// 主列表数据
        /// </summary>
        public ObservableCollection<RecipeTable> DataCollection
        {
            get { return _DataCollection; }
            set
            {
                _DataCollection = value;
                RaisePropertyChanged("DataCollection");
            }
        }
        /// <summary>
        /// 搜索条件
        /// </summary>
        private RecipeTableByVerification _Search = new RecipeTableByVerification();
        /// <summary>
        /// 搜索条件
        /// </summary>
        public RecipeTableByVerification Search
        {
            get { return _Search; }
            set
            {
                _Search = value;
                RaisePropertyChanged("Search");
            }

        }
        /// <summary>
        /// 当前查看的配方实体类
        /// </summary>
        private RecipeTable _CurrentModel = new RecipeTable();
        /// <summary>
        /// 当前查看的配方实体类
        /// </summary>
        public RecipeTable CurrentModel
        {
            get { return _CurrentModel; }
            set
            {
                _CurrentModel = value;
                RaisePropertyChanged("CurrentModel");
            }

        }
        /// <summary>
        /// 搜索命令
        /// </summary>
        public DelegateCommand SearchCommand { get; set; }

        /// <summary>
        /// 创建命令
        /// </summary>
        public DelegateCommand CreateCommand { get; set; }

        /// <summary>
        /// 显示明细页面命令
        /// </summary>
        public DelegateCommand ShowDetalModelCommand { get; set; }

        /// <summary>
        /// 显示编辑页面命令
        /// </summary>
        public DelegateCommand ShowEditModelCommand { get; set; }

        /// <summary>
        /// 显示删除模态窗体命令
        /// </summary>
        public DelegateCommand ShowDeleteModelCommand { get; set; }

        /// <summary>
        /// 显示日历窗体命令
        /// </summary>
        public DelegateCommand ShowCalendarCommand { get; set; }

        /// <summary>
        /// 添加类别命令
        /// </summary>
        public DelegateCommand AddTypeConditionsCommand { get; set; }

        /// <summary>
        /// 类别搜索条件
        /// </summary>
        private List<string> _TypeConditions = new List<string>();
        /// <summary>
        /// 类别搜索条件
        /// </summary>
        public List<string> TypeConditions
        {
            get { return _TypeConditions; }
            set
            {
                _TypeConditions = value;
                RaisePropertyChanged("TypeConditions");
            }
        }

        public RecipeManagementPageViewModel()
        {
            InitDataCollection();
            SearchCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(SearchHandler)
            };
            CreateCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CreateHandler)
            };

            ShowCalendarCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ShowCalendarHandler)
            };

            ShowDetalModelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ShowDetalModelHandler)
            };
            CancelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CancelHandler)
            };
            ShowEditModelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ShowEditModelHandler)
            };
            ShowDeleteModelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ShowDeleteModelHandler)
            };

            ConfirmDeleteCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ConfirmDeleteHandler)
            };
            CancelDeleteCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CancelDeleteHandler)
            };
            AddTypeConditionsCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(AddTypeConditionsHandler)
            };
            GoToPageByNumberCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(GoToPageByNumberHandler)
            };
            GoToPageByNumberAndCloseAllPageNumberPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(GoToPageByNumberAndCloseAllPageNumberPanelHandler)
            };
            ChangeCurrentPageNumberCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ChangeCurrentPageNumberHandler)
            };
            //重新加载数据
            Messenger.Default.Register<string>(this, "ReLoadRecipeManagementPageData", (message) =>
            {
                Search = new RecipeTableByVerification();
                Search.MSC_DateCreated_Start = "点击设置开始时间";
                Search.MSC_DateCreated_End = "点击设置结束时间";
                InitDataCollection();
            });
            //获取传递过来的时间参数开始
            Messenger.Default.Register<string>(this, "SetStartDate", (message) =>
            {
                Search.MSC_DateCreated_Start = message;
            });

            //获取传递过来的时间参数结束
            Messenger.Default.Register<string>(this, "SetEndDate", (message) =>
            {
                Search.MSC_DateCreated_End = message;
            });

            //获取传递过来要删除的配方ID
            Messenger.Default.Register<string>(this, "SetDeleteRecipeId", (message) =>
            {
                DeleteSource = "RecipeSelectionPage";
                DeleteRecipeId = int.Parse(message);
            });

            Search.MSC_DateCreated_Start = "点击设置开始时间";
            Search.MSC_DateCreated_End = "点击设置结束时间";
        }

        /// <summary>
        /// 跳转到更改页码的列表
        /// </summary>
        /// <param name="obj"></param>
        private void GoToPageByNumberAndCloseAllPageNumberPanelHandler(object obj)
        {
            GoToPageByNumberHandler(obj);
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideAllPageNumberPanelForRecipeManage"));
        }
        /// <summary>
        /// 打开更改页码窗体
        /// </summary>
        /// <param name="obj"></param>
        private void ChangeCurrentPageNumberHandler(object obj)
        {
            PageNumers = new ObservableCollection<PageNumerModel>();
            for (int i = 0; i < TotalPageNumber; i++)
            {
                PageNumers.Add(new PageNumerModel() { PageNumberStr = (i + 1).ToString() });
            }
            if (PageNumers.Count > 1)
            {
                WindowManager.ShowDialog("AllPageNumberPanelForRecipeManageWindow", "");
            }
        }
        /// <summary>
        /// 跳转到指定页面命令的处理器
        /// </summary>
        /// <param name="obj"></param>
        private void GoToPageByNumberHandler(object obj)
        {
            switch (obj.ToString())
            {
                case "PreviousPage":
                    CurrentPageNumber = PreviousPageNumber;
                    break;
                case "NextPage":
                    CurrentPageNumber = NextPageNumber;
                    break;
                case "FirstPage":
                    CurrentPageNumber = 1;
                    break;
                case "LastPage":
                    CurrentPageNumber = TotalPageNumber;
                    break;
                default:
                    CurrentPageNumber = int.Parse(obj.ToString());
                    break;
            }
            InitDataCollection();
        }
        /// <summary>
        /// 添加类别条件
        /// </summary>
        /// <param name="obj"></param>
        private void AddTypeConditionsHandler(object obj)
        {
            if (obj != null)
            {
                bool IsHave = false;

                for (int i = 0; i < TypeConditions.Count; i++)
                {
                    if (TypeConditions[i] == obj.ToString())
                    {
                        IsHave = true;
                        break;
                    }
                }
                if (IsHave)
                {
                    TypeConditions.Remove(obj.ToString());
                }
                else
                {
                    TypeConditions.Add(obj.ToString());
                }
            }
        }
        /// <summary>
        /// 取消命令处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void CancelHandler(object obj)
        {
            //重新加载数据
            Messenger.Default.Send<string>("", "ReLoadRecipeManagementPageData");
            //跳转到配方管理页
            Messenger.Default.Send<string>("RecipeManagementPage", "GoToPage");
        }
        /// <summary>
        /// 跳转到显示页面
        /// </summary>
        /// <param name="obj"></param>
        private void ShowDetalModelHandler(object obj)
        {
            if (obj == null)
            {
                MessageBox.Show("请选择要查看的配方","警告");
                return;
            }
            string QueryOneSql = @"SELECT   a.RecipeId
                                           ,a.RecipeName
                                           ,a.MSC_REV
                                           ,a.MSC_MoldTech
                                           ,a.MSC_Alloy
                                           ,a.MSC_MOLDDIAMETER
                                           ,a.MSC_NUMBERMOLDS
                                           ,a.MSC_Description
                                           ,b.UserName as MSC_Creater
                                           ,CONVERT(NVARCHAR(255),a.MSC_DateCreated,120) AS MSC_DateCreatedStr
                                           ,CONVERT(NVARCHAR(255),a.MSC_DateLastUsed,120) AS MSC_DateLastUsedStr
                                           ,a.FRN_FRNBATHTEMPHILIM
                                           ,a.FRN_FRNBATHTEMPLOLIM
                                           ,a.FRN_FRNBATHTEMPSP
                                           ,a.FRN_FRNTILTBACKOFFSET
                                           ,a.GAS_BEGINRAMPDOWN
                                           ,a.GAS_ENDRAMPDOWN
                                           ,a.GAS_IDLECASTGAS
                                           ,a.GAS_RUNCASTGAS
                                           ,a.GAS_STARTCASTGAS
                                           ,a.GAS_STARTCASTGASLEN
                                           ,a.JET_OFFPRESLEN
                                           ,a.JET_OFFPRESLENEND
                                           ,a.JET_PRES1
                                           ,a.JET_PRES1END
                                           ,a.JET_PRES2
                                           ,a.JET_PRES2END
                                           ,a.JET_PRESLEN1
                                           ,a.JET_PRESLEN1END
                                           ,a.JET_PRESLEN2
                                           ,a.JET_PRESLEN2END
                                           ,a.JET_STARTPRES
                                           ,a.JET_STARTPRESEND
                                           ,a.LEN_CASTLENGTH
                                           ,a.LUB_LEN2
                                           ,a.LUB_LEN3
                                           ,a.LUB_LEN4
                                           ,a.LUB_PULSELUBECYCTIME
                                           ,a.LUB_PULSELUBECYCTIME2
                                           ,a.LUB_PULSELUBECYCTIME3
                                           ,a.LUB_PULSELUBECYCTIME4
                                           ,a.LUB_PULSELUBEOFFTIME
                                           ,a.LUB_PULSELUBEONTIME
                                           ,a.LUB_PULSELUBEPRESFIRE
                                           ,a.LUB_PULSELUBEPRESHOLD
                                           ,a.LUB_PULSELUBEPRESRESET
                                           ,a.LUB_PULSELUBEPULSES
                                           ,a.LUB_PULSELUBEPULSES2
                                           ,a.LUB_PULSELUBEPULSES3
                                           ,a.LUB_PULSELUBEPULSES4
                                           ,a.MLC_FILLRATE1
                                           ,a.MLC_FILLRATE1STARTLEVEL
                                           ,a.MLC_FILLRATE2
                                           ,a.MLC_FILLRATE2STARTLEVEL
                                           ,a.MLC_FILLRATE3
                                           ,a.MLC_FILLRATE3STARTLEVEL
                                           ,a.MLC_FILLRATE4
                                           ,a.MLC_FILLRATE4STARTLEVEL
                                           ,a.MLC_FILLRATESTART
                                           ,a.MLC_MOLDLVL1
                                           ,a.MLC_MOLDLVL1LEN
                                           ,a.MLC_MOLDLVL2
                                           ,a.MLC_MOLDLVL2LEN
                                           ,a.MLC_MOLDLVL3
                                           ,a.MLC_MOLDLVL3LEN
                                           ,a.MLC_MOLDLVL4
                                           ,a.MLC_MOLDLVL4LEN
                                           ,a.MLC_MOLDLVL5
                                           ,a.MLC_MOLDLVL5LEN
                                           ,a.MLC_MOLDLVL6
                                           ,a.MLC_MOLDLVL6LEN
                                           ,a.MLC_MOLDLVL7
                                           ,a.MLC_MOLDLVL7LEN
                                           ,a.MLC_MOLDLVL8
                                           ,a.MLC_MOLDLVL8LEN
                                           ,a.MLC_MOLDLVL9
                                           ,a.MLC_MOLDLVL9LEN
                                           ,a.MLC_MOLDLVLSTART
                                           ,a.MLC_PINPRESET1
                                           ,a.MLC_PINPRESET2
                                           ,a.MLC_PINPRESET3
                                           ,a.MLC_PINPRESET4
                                           ,a.MLC_PINPRESET5
                                           ,a.MLC_PINPRESET6
                                           ,a.MLC_PINPRESET7
                                           ,a.MLC_PINPRESET8
                                           ,a.MLC_PINPRESET9
                                           ,a.MLC_PINPRESET10
                                           ,a.MLC_PINPRESET11
                                           ,a.MLC_PINPRESET12
                                           ,a.MLC_PINPRESET13
                                           ,a.MLC_PINPRESET14
                                           ,a.MLC_PINPRESET15
                                           ,a.MLC_UNCNTRLDFILLDIST
                                           ,a.MTL_DEGASTEMP
                                           ,a.ROD_RODSPEED
                                           ,a.SPD_CASTSPD1
                                           ,a.SPD_CASTSPD1LEN
                                           ,a.SPD_CASTSPD2
                                           ,a.SPD_CASTSPD2LEN
                                           ,a.SPD_CASTSPD3
                                           ,a.SPD_CASTSPD3LEN
                                           ,a.SPD_CASTSPD4
                                           ,a.SPD_CASTSPD4LEN
                                           ,a.SPD_CASTSPD5
                                           ,a.SPD_CASTSPD5LEN
                                           ,a.SPD_CASTSPD6
                                           ,a.SPD_CASTSPD6LEN
                                           ,a.SPD_CASTSPD7
                                           ,a.SPD_CASTSPD7LEN
                                           ,a.SPD_CASTSPD8
                                           ,a.SPD_CASTSPD8LEN
                                           ,a.SPD_CASTSPD9
                                           ,a.SPD_CASTSPD9LEN
                                           ,a.SPD_CASTSPDEND
                                           ,a.SPD_CASTSPDSTART
                                           ,a.SPD_PLTNHOLDTIME
                                           ,a.TGH_DAMPULLSP1
                                           ,a.TGH_DAMPULLSP2
                                           ,a.TGH_TROUGHTEMP
                                           ,a.WIP_WiperOnLen
                                           ,a.WIP_WiperPosition
                                           ,a.WIP_WIPPOS1
                                           ,a.WIP_WIPPOS1LEN
                                           ,a.WIP_WIPPOS2
                                           ,a.WIP_WIPPOS2LEN
                                           ,a.WIP_WIPPOS3
                                           ,a.WIP_WIPPOS3LEN
                                           ,a.WIP_WIPPOS4
                                           ,a.WIP_WIPPOS4LEN
                                           ,a.WIP_WIPPOS5
                                           ,a.WIP_WIPPOS5LEN
                                           ,a.WIP_WIPPOS6
                                           ,a.WIP_WIPPOS6LEN
                                           ,a.WIP_WIPPOS7
                                           ,a.WIP_WIPPOS7LEN
                                           ,a.WIP_WIPPOS8
                                           ,a.WIP_WIPPOS8LEN
                                           ,a.WIP_WIPPOS9
                                           ,a.WIP_WIPPOS9LEN
                                           ,a.WIP_WIPPOSEND
                                           ,a.WIP_WIPPOSSTART
                                           ,a.WTR_WATEROFFDELAY
                                           ,a.WTR_WTRFLOW1
                                           ,a.WTR_WTRFLOW1LEN
                                           ,a.WTR_WTRFLOW2
                                           ,a.WTR_WTRFLOW2LEN
                                           ,a.WTR_WTRFLOW3
                                           ,a.WTR_WTRFLOW3LEN
                                           ,a.WTR_WTRFLOW4
                                           ,a.WTR_WTRFLOW4LEN
                                           ,a.WTR_WTRFLOW5
                                           ,a.WTR_WTRFLOW5LEN
                                           ,a.WTR_WTRFLOW6
                                           ,a.WTR_WTRFLOW6LEN
                                           ,a.WTR_WTRFLOW7
                                           ,a.WTR_WTRFLOW7LEN
                                           ,a.WTR_WTRFLOW8
                                           ,a.WTR_WTRFLOW8LEN
                                           ,a.WTR_WTRFLOW9
                                           ,a.WTR_WTRFLOW9LEN
                                           ,a.WTR_WTRFLOWEND
                                           ,a.WTR_WTRFLOWSTART
                                           ,a.WTR_WTRTEMPHILIMIT
                                           ,a.WTR_WTRTEMPLOLIMIT
                                           ,a.MSC_COOLDOWNTIME
                                           ,a.MSC_MOLDHEIGHT
                                           ,a.MSC_MOLDTHICKNESS
                                           ,a.MSC_MOLDWIDTH
                                           ,a.MSC_RECNAME
                                           ,a.MSC_UserDef1
                                           ,a.MSC_UserDef2
                                           ,a.MSC_UserDef3
                                           ,a.MSC_UserDef4
                                           ,a.MSC_UserDef5
                                           ,a.MSC_UserDef6
                                           ,a.MSC_UserDef7
                                           ,a.MSC_UserDef8
                                           ,a.MSC_UserDef9
                                           ,a.MSC_UserDef10
                                           ,a.MSC_Type
                                     FROM RecipeTable a
                                     LEFT JOIN [User] b ON a.MSC_CreatedBy = b.UserId WHERE a.RecipeId = @RecipeId ";
            RecipeTable QueryModel = new RecipeTable();
            QueryModel.RecipeId = int.Parse(obj.ToString());
            CurrentModel = DapperToolsThisSystem.Query<RecipeTable>(QueryOneSql, QueryModel);
            Messenger.Default.Send("ViewRecipePage", "GoToPage");
        }
        /// <summary>
        /// 显示日历窗体命令处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void ShowCalendarHandler(object obj)
        {
            WindowManager.ShowModalCalendar("CalendarWindow", obj.ToString());
        }
        /// <summary>
        /// 搜索命令处理器方法
        /// </summary>
        /// <param name="obj"></param>
        private void SearchHandler(object obj)
        {
            InitDataCollection();
        }
        /// <summary>
        /// 创建命令处理器方法
        /// </summary>
        /// <param name="obj"></param>
        private void CreateHandler(object obj)
        {
            Messenger.Default.Send("CreateRecipePage", "GoToPage");
        }
        /// <summary>
        /// 跳转到编辑页面命令的处理器
        /// </summary>
        /// <param name="obj"></param>
        private void ShowEditModelHandler(object obj)
        {
            if (obj == null)
            {
                MessageBox.Show("请选择要编辑的配方", "警告");
                return;
            }
            string QueryOneSql = @"SELECT   a.RecipeId
                                           ,a.RecipeName
                                           ,a.MSC_REV
                                           ,a.MSC_MoldTech
                                           ,a.MSC_Alloy
                                           ,a.MSC_MOLDDIAMETER
                                           ,a.MSC_NUMBERMOLDS
                                           ,a.MSC_Description
                                           ,b.UserName as MSC_Creater
                                           ,CONVERT(NVARCHAR(255),a.MSC_DateCreated,120) AS MSC_DateCreatedStr
                                           ,CONVERT(NVARCHAR(255),a.MSC_DateLastUsed,120) AS MSC_DateLastUsedStr
                                           ,a.FRN_FRNBATHTEMPHILIM
                                           ,a.FRN_FRNBATHTEMPLOLIM
                                           ,a.FRN_FRNBATHTEMPSP
                                           ,a.FRN_FRNTILTBACKOFFSET
                                           ,a.GAS_BEGINRAMPDOWN
                                           ,a.GAS_ENDRAMPDOWN
                                           ,a.GAS_IDLECASTGAS
                                           ,a.GAS_RUNCASTGAS
                                           ,a.GAS_STARTCASTGAS
                                           ,a.GAS_STARTCASTGASLEN
                                           ,a.JET_OFFPRESLEN
                                           ,a.JET_OFFPRESLENEND
                                           ,a.JET_PRES1
                                           ,a.JET_PRES1END
                                           ,a.JET_PRES2
                                           ,a.JET_PRES2END
                                           ,a.JET_PRESLEN1
                                           ,a.JET_PRESLEN1END
                                           ,a.JET_PRESLEN2
                                           ,a.JET_PRESLEN2END
                                           ,a.JET_STARTPRES
                                           ,a.JET_STARTPRESEND
                                           ,a.LEN_CASTLENGTH
                                           ,a.LUB_LEN2
                                           ,a.LUB_LEN3
                                           ,a.LUB_LEN4
                                           ,a.LUB_PULSELUBECYCTIME
                                           ,a.LUB_PULSELUBECYCTIME2
                                           ,a.LUB_PULSELUBECYCTIME3
                                           ,a.LUB_PULSELUBECYCTIME4
                                           ,a.LUB_PULSELUBEOFFTIME
                                           ,a.LUB_PULSELUBEONTIME
                                           ,a.LUB_PULSELUBEPRESFIRE
                                           ,a.LUB_PULSELUBEPRESHOLD
                                           ,a.LUB_PULSELUBEPRESRESET
                                           ,a.LUB_PULSELUBEPULSES
                                           ,a.LUB_PULSELUBEPULSES2
                                           ,a.LUB_PULSELUBEPULSES3
                                           ,a.LUB_PULSELUBEPULSES4
                                           ,a.MLC_FILLRATE1
                                           ,a.MLC_FILLRATE1STARTLEVEL
                                           ,a.MLC_FILLRATE2
                                           ,a.MLC_FILLRATE2STARTLEVEL
                                           ,a.MLC_FILLRATE3
                                           ,a.MLC_FILLRATE3STARTLEVEL
                                           ,a.MLC_FILLRATE4
                                           ,a.MLC_FILLRATE4STARTLEVEL
                                           ,a.MLC_FILLRATESTART
                                           ,a.MLC_MOLDLVL1
                                           ,a.MLC_MOLDLVL1LEN
                                           ,a.MLC_MOLDLVL2
                                           ,a.MLC_MOLDLVL2LEN
                                           ,a.MLC_MOLDLVL3
                                           ,a.MLC_MOLDLVL3LEN
                                           ,a.MLC_MOLDLVL4
                                           ,a.MLC_MOLDLVL4LEN
                                           ,a.MLC_MOLDLVL5
                                           ,a.MLC_MOLDLVL5LEN
                                           ,a.MLC_MOLDLVL6
                                           ,a.MLC_MOLDLVL6LEN
                                           ,a.MLC_MOLDLVL7
                                           ,a.MLC_MOLDLVL7LEN
                                           ,a.MLC_MOLDLVL8
                                           ,a.MLC_MOLDLVL8LEN
                                           ,a.MLC_MOLDLVL9
                                           ,a.MLC_MOLDLVL9LEN
                                           ,a.MLC_MOLDLVLSTART
                                           ,a.MLC_PINPRESET1
                                           ,a.MLC_PINPRESET2
                                           ,a.MLC_PINPRESET3
                                           ,a.MLC_PINPRESET4
                                           ,a.MLC_PINPRESET5
                                           ,a.MLC_PINPRESET6
                                           ,a.MLC_PINPRESET7
                                           ,a.MLC_PINPRESET8
                                           ,a.MLC_PINPRESET9
                                           ,a.MLC_PINPRESET10
                                           ,a.MLC_PINPRESET11
                                           ,a.MLC_PINPRESET12
                                           ,a.MLC_PINPRESET13
                                           ,a.MLC_PINPRESET14
                                           ,a.MLC_PINPRESET15
                                           ,a.MLC_UNCNTRLDFILLDIST
                                           ,a.MTL_DEGASTEMP
                                           ,a.ROD_RODSPEED
                                           ,a.SPD_CASTSPD1
                                           ,a.SPD_CASTSPD1LEN
                                           ,a.SPD_CASTSPD2
                                           ,a.SPD_CASTSPD2LEN
                                           ,a.SPD_CASTSPD3
                                           ,a.SPD_CASTSPD3LEN
                                           ,a.SPD_CASTSPD4
                                           ,a.SPD_CASTSPD4LEN
                                           ,a.SPD_CASTSPD5
                                           ,a.SPD_CASTSPD5LEN
                                           ,a.SPD_CASTSPD6
                                           ,a.SPD_CASTSPD6LEN
                                           ,a.SPD_CASTSPD7
                                           ,a.SPD_CASTSPD7LEN
                                           ,a.SPD_CASTSPD8
                                           ,a.SPD_CASTSPD8LEN
                                           ,a.SPD_CASTSPD9
                                           ,a.SPD_CASTSPD9LEN
                                           ,a.SPD_CASTSPDEND
                                           ,a.SPD_CASTSPDSTART
                                           ,a.SPD_PLTNHOLDTIME
                                           ,a.TGH_DAMPULLSP1
                                           ,a.TGH_DAMPULLSP2
                                           ,a.TGH_TROUGHTEMP
                                           ,a.WIP_WiperOnLen
                                           ,a.WIP_WiperPosition
                                           ,a.WIP_WIPPOS1
                                           ,a.WIP_WIPPOS1LEN
                                           ,a.WIP_WIPPOS2
                                           ,a.WIP_WIPPOS2LEN
                                           ,a.WIP_WIPPOS3
                                           ,a.WIP_WIPPOS3LEN
                                           ,a.WIP_WIPPOS4
                                           ,a.WIP_WIPPOS4LEN
                                           ,a.WIP_WIPPOS5
                                           ,a.WIP_WIPPOS5LEN
                                           ,a.WIP_WIPPOS6
                                           ,a.WIP_WIPPOS6LEN
                                           ,a.WIP_WIPPOS7
                                           ,a.WIP_WIPPOS7LEN
                                           ,a.WIP_WIPPOS8
                                           ,a.WIP_WIPPOS8LEN
                                           ,a.WIP_WIPPOS9
                                           ,a.WIP_WIPPOS9LEN
                                           ,a.WIP_WIPPOSEND
                                           ,a.WIP_WIPPOSSTART
                                           ,a.WTR_WATEROFFDELAY
                                           ,a.WTR_WTRFLOW1
                                           ,a.WTR_WTRFLOW1LEN
                                           ,a.WTR_WTRFLOW2
                                           ,a.WTR_WTRFLOW2LEN
                                           ,a.WTR_WTRFLOW3
                                           ,a.WTR_WTRFLOW3LEN
                                           ,a.WTR_WTRFLOW4
                                           ,a.WTR_WTRFLOW4LEN
                                           ,a.WTR_WTRFLOW5
                                           ,a.WTR_WTRFLOW5LEN
                                           ,a.WTR_WTRFLOW6
                                           ,a.WTR_WTRFLOW6LEN
                                           ,a.WTR_WTRFLOW7
                                           ,a.WTR_WTRFLOW7LEN
                                           ,a.WTR_WTRFLOW8
                                           ,a.WTR_WTRFLOW8LEN
                                           ,a.WTR_WTRFLOW9
                                           ,a.WTR_WTRFLOW9LEN
                                           ,a.WTR_WTRFLOWEND
                                           ,a.WTR_WTRFLOWSTART
                                           ,a.WTR_WTRTEMPHILIMIT
                                           ,a.WTR_WTRTEMPLOLIMIT
                                           ,a.MSC_COOLDOWNTIME
                                           ,a.MSC_MOLDHEIGHT
                                           ,a.MSC_MOLDTHICKNESS
                                           ,a.MSC_MOLDWIDTH
                                           ,a.MSC_RECNAME
                                           ,a.MSC_UserDef1
                                           ,a.MSC_UserDef2
                                           ,a.MSC_UserDef3
                                           ,a.MSC_UserDef4
                                           ,a.MSC_UserDef5
                                           ,a.MSC_UserDef6
                                           ,a.MSC_UserDef7
                                           ,a.MSC_UserDef8
                                           ,a.MSC_UserDef9
                                           ,a.MSC_UserDef10
                                           ,a.MSC_Type
                                     FROM RecipeTable a
                                     LEFT JOIN [User] b ON a.MSC_CreatedBy = b.UserId WHERE a.RecipeId = @RecipeId ";
            RecipeTableByVerification QueryModel = new RecipeTableByVerification();
            QueryModel.RecipeId = int.Parse(obj.ToString());
            SystemConfig._EditModel = DapperToolsThisSystem.Query<RecipeTableByVerification>(QueryOneSql, QueryModel);
            Messenger.Default.Send("", "Refresh");
            Messenger.Default.Send("EditRecipePage", "GoToPage");
        }

        /// <summary>
        /// 初始化主列表数据
        /// </summary>
        private void InitDataCollection()
        {
            string baseSearchSql = @"SELECT ROW_NUMBER() OVER (
                                                                ORDER BY a.MSC_DateCreated
                                                               ) as rownumber,
                                            a.RecipeId
                                           ,a.RecipeName
                                           ,a.MSC_REV
                                           ,a.MSC_MoldTech
                                           ,a.MSC_Alloy
                                           ,a.MSC_MOLDDIAMETER
                                           ,a.MSC_NUMBERMOLDS
                                           ,a.MSC_Description
                                           ,b.UserName as MSC_Creater
                                           ,CONVERT(NVARCHAR(255),a.MSC_DateCreated,120) AS MSC_DateCreatedStr
                                           ,CONVERT(NVARCHAR(255),a.MSC_DateLastUsed,120) AS MSC_DateLastUsedStr
                                     FROM RecipeTable a
                                     LEFT JOIN [User] b ON a.MSC_CreatedBy = b.UserId WHERE 1 = 1 ";
            if (Search.RecipeName != null && Search.RecipeName.Trim().Length > 0)
            {
                baseSearchSql += " AND RecipeName LIKE '%" + Search.RecipeName + "%'";
            }

            if (Search.MSC_MoldTech != null && Search.MSC_MoldTech.Trim().Length > 0)
            {
                baseSearchSql += " AND MSC_MoldTech LIKE '%" + Search.MSC_MoldTech + "%'";
            }

            if (Search.MSC_Alloy != null && Search.MSC_Alloy.Trim().Length > 0)
            {
                baseSearchSql += " AND MSC_Alloy LIKE '%" + Search.MSC_Alloy + "%'";
            }

            if (Search.MSC_REVStr != null && Search.MSC_REVStr.Trim().Length > 0)
            {
                baseSearchSql += " AND MSC_REV  = " + Search.MSC_REVStr;
            }

            if (Search.MSC_MOLDDIAMETERStr != null && Search.MSC_MOLDDIAMETERStr.Trim().Length > 0)
            {
                baseSearchSql += " AND MSC_MOLDDIAMETER  = " + Search.MSC_MOLDDIAMETERStr;
            }
            if (Search.MSC_DateCreated_Start != null && Search.MSC_DateCreated_Start.Trim().Length > 0 && Search.MSC_DateCreated_Start != "点击设置开始时间")
            {
                baseSearchSql += " AND MSC_DateCreated  >= '" + Search.MSC_DateCreated_Start + "'";
            }
            if (Search.MSC_DateCreated_End != null && Search.MSC_DateCreated_End.Trim().Length > 0 && Search.MSC_DateCreated_End != "点击设置结束时间")
            {
                baseSearchSql += " AND MSC_DateCreated  < '" + Search.MSC_DateCreated_End + "'";
            }
            for (int i = 0; i < TypeConditions.Count; i++)
            {
                if (i == 0)
                {
                    baseSearchSql += " AND ( MSC_Type = '" + TypeConditions[i] + "'";
                }
                else
                {
                    baseSearchSql += " OR MSC_Type = '" + TypeConditions[i] + "'";
                }
                if (i == TypeConditions.Count - 1)
                {
                    baseSearchSql += " ) ";
                }
            }
            string pageingSql = @"SELECT TOP " + itemCount + @" * 
                                  FROM (" + baseSearchSql + @") temp_row
                                  WHERE rownumber > ((" + CurrentPageNumber + @" - 1 ) * " + itemCount + @" ); ";
            DataCollection = DapperToolsThisSystem.QueryOc<RecipeTable>(pageingSql);
            //总数据量
            TotalDataCount = DapperToolsThisSystem.GetCount(baseSearchSql);
            for (int i = 0; i < DataCollection.Count; i++)
            {
                DataCollection[i].OrderNo = (i + 1);
            }
        }

        /// <summary>
        /// 删除的配方ID
        /// </summary>
        private int DeleteRecipeId;
        /// <summary>
        /// 显示删除确认窗体
        /// </summary>
        /// <param name="obj"></param>
        private void ShowDeleteModelHandler(object obj)
        {
            if (obj == null)
            {
                MessageBox.Show("请选择要删除的配方", "警告");
                return;
            }
            DeleteRecipeId = (int)obj;
            DeleteSource = "RecipeManagement";
            WindowManager.ShowDialog("DeleteRecipeWindow", "");
        }

        /// <summary>
        /// 确认删除命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void ConfirmDeleteHandler(object obj)
        {
            string deleteSql = "DELETE RecipeTable WHERE RecipeId = @RecipeId";
            RecipeTable deleteRecipeTable = new RecipeTable
            {
                RecipeId = DeleteRecipeId
            };
            if (DapperToolsThisSystem.Delete<RecipeTable>(deleteSql, deleteRecipeTable) > 0)
            {
                InitDataCollection();
            }
            CancelDeleteCommand.Execute(null);
        }
        /// <summary>
        /// 关闭删除窗体
        /// </summary>
        /// <param name="obj"></param>
        private void CancelDeleteHandler(object obj)
        {
            if (DeleteSource == "RecipeSelectionPage")
            {
                Messenger.Default.Send("", "RecipeSelectionPageRefurbish");
            }
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideDeleteRecipeWindow"));
        }
    }
}
